local skill = fk.CreateSkill{
  name = "tea__yishu",
}
local U = require "packages/utility/utility"
local isBetween = function(num1, num2, num)
  return (num1 < num and num2 > num) or (num1 > num and num2 < num)
end
---@class ViewAsSkill
---@field cost_data any
---@type ViewAsSkillSpec
local yishu = {
  mute = true,
  pattern = "slash",
  prompt = "#tea__yishu-invoke",
  interaction = function(self, player)
    local all_names = {"tea_shot__slash"}
    local names = player:getViewAsCardNames("tea__yishu", all_names)
    if #names > 0 then
      return UI.CardNameBox { choices = names, all_choices = all_names }
    end
  end,
  card_filter = function(self, player, to_select, selected)
    if table.contains(Self:getCardIds("h"), to_select) then
      if #selected == 0 then return true
      elseif #selected == 1 then
        return table.find(Self:getCardIds("h"), function(id)
          return isBetween(Fk:getCardById(selected[1]).number, Fk:getCardById(to_select).number, Fk:getCardById(id).number)
        end)
      end
    end
  end,
  view_as = function(self, player, cards)
    if not self.interaction.data or #cards ~= 2 then return end
    local card = Fk:cloneCard(self.interaction.data)
    self.cost_data = cards --fixme
    card.skillName = skill.name
    return card
  end,
  enabled_at_play = function(self, player)
    return #player:getCardIds("h") > 1
  end,
  enabled_at_response = function(self, player, response)
    return not response and #player:getCardIds("h") > 1
  end,
  before_use = function(self, player, use)
    local cards = self.cost_data
    local room = player.room
    if #cards == 2 then
      local num1 = Fk:getCardById(cards[1]).number
      local num2 = Fk:getCardById(cards[2]).number
      local ids = table.filter(player:getCardIds("h"), function(id)
        local num = Fk:getCardById(id).number
        local virtual_card = Fk:cloneCard("tea_shot__slash")
        virtual_card.skillName = skill.name
        virtual_card:addSubcard(id)
        return isBetween(num1, num2, num) and #virtual_card:getDefaultTarget(player) > 0 --提前做点小判定
      end)
      if #ids > 0 then
        local cid = room:askToCards(player, {
          min_num = 1,
          max_num = 1,
          include_equip = true,
          skill_name = skill.name,
          pattern = tostring(Exppattern{ id = ids }),
          prompt = "#tea__yishu-choose"
        })
        if #cid == 1 then
          player:showCards(cards)
          if #player:getPile("$tea__tongzhi") == 2 then
            local cnum1 = Fk:getCardById(player:getPile("$tea__tongzhi")[1]).number
            local cnum2 = Fk:getCardById(player:getPile("$tea__tongzhi")[2]).number
            if isBetween(cnum1, cnum2, Fk:getCardById(cid[1]).number) then
              local tos = use.tos
              room:damage{
                to = player,
                damage = 1,
                skillName = skill.name,
              }
              room:sortByAction(tos)
              for _, to in ipairs(tos) do
                room:damage{
                  to = to,
                  damage = 1,
                  skillName = skill.name,
                }
              end
              tos = table.filter(tos, function(p)
                return not p.dead
              end)
              if #tos == 0 then 
                return skill.name
              else
                use.tos = tos
              end
            end
          end
          room:notifySkillInvoked(player, skill.name, "special")
          player:broadcastSkillInvoke(skill.name)
          use.card:addSubcards(cid)
          return
        end
      end
    end
    return skill.name
  end,
}
skill:addEffect("viewas", yishu)
return skill